package snake;

import javax.swing.*;
import java.awt.*;

//

public class SnakeApp implements Runnable{

    @Override
    public void run(){

        //游戏窗体的设置
        JFrame window = new JFrame("贪吃蛇游戏");
        window.setLocation(400,200);
        window.pack();
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);

        Grid grid = null;
        try {
            grid = new Grid(Settings.DEFUALT_GRID_WIDTH, Settings.DEFUALT_GRID_HEIGHT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        GameView gameView = new GameView(grid);
        Controller controller = new Controller(grid,gameView);

        Container contentPane = window.getContentPane();
        gameView.init();
        gameView.getCanvas().setPreferredSize(new Dimension(Settings.DEFUALT_GRID_WIDTH*Settings.DEFUALT_NODE_SiZE,Settings.DEFUALT_GRID_HEIGHT*Settings.DEFUALT_NODE_SiZE));
        contentPane.add(gameView.getCanvas(), BorderLayout.CENTER);
        window.addKeyListener(controller);
        window.pack();
        new Thread(controller).start();

    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SnakeApp());
    }
}
